<?php
$this->pageTitle = 'Thông báo';
$this->breadcrumbs = array(
	'Thông báo'=>array('index'),
);

?>
<?php
$form = $this->beginWidget('CActiveForm', array(
				'enableAjaxValidation'=>false,
				'id'=>'announcement_form',
			));?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/header.ico" /> Thông báo</h1>

    <div class="controls">
    	<div class="seachDate">
			Từ
				<?php echo CHtml::textField('Statistic[start_year]','',array('id' => 'start_year', 'class'=> 'picker-monthyear', 'style' => 'z-index:0;')); ?>
			đến
				<?php echo CHtml::textField('Statistic[end_year]','',array('id' => 'end_year', 'class'=> 'picker-monthyear', 'style' => 'z-index:0;')); ?>
		</div>
        <?php
        echo CHtml::submitButton('Thống kê tổng quát', array('class' => 'controls', 'name' => 'announcement_excel'));
        echo CHtml::submitButton('Thống kê chi tiết', array('class' => 'controls', 'name' => 'announcement_detail_excel'));
        echo CHtml::button('Đăng kí', array('class' => 'controls', 'id' => 'create-announcement'));
        echo CHtml::button('Xóa', array('class' => 'controls', 'id' => 'delete-announcement'));
        ?>
    </div>
</div>

<div class="content clear">
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'announcement-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
    'cssFile' => Util::themeUrl() . '/css/gridview.css',
    'htmlOptions'=>array('class'=>'grid-view clear'),
    'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
		array(
            'header' =>  'No.',
            'value' => '$row + Util::getPaggingIndex()',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => '$data->id',
        ),
		array(
		'name'	=>'title',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"title")',
		'htmlOptions' => array(
                'style' => 'width:300px;')
		),
		/*array(
		'name'	=>'description',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"description")',
		),*/
		array(
		'name'			=>'event_fg',
      	'type' 			=> 'raw',
      	'value' 		=> 'CHtml::image(Util::themeUrl(). "/images/" . (($data->event_fg==STATUS_ACTIVE)? "mail.jpg" : "thongbao.jpg"), "",  array("width" => 40, "height" => 30))',
		'filter' 		=> Util::itemAlias('Event'),
      	'htmlOptions' 	=> array(
                'style' => 'width:50px;text-align:center',
            )
		),
		array(
		'name'	=>'event_end_date',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"event_end_date")',
		'htmlOptions' => array(
                'style' => 'width:120px;')
		),
		array(
		'name'	=>'status',
		'type'	=>'raw',
		'value'	=>'Util::getValueFromAlias($data->status,"Status")',
		'filter' => Util::itemAlias('Status'),
		'htmlOptions' => array(
                'style' => 'width:60px;')
		),
		/*array(
		'name'	=>'position',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"position")',
		'htmlOptions' => array(
                'style' => 'width:50px;')
		),*/
		array(
		'name'	=>'comment',
		'type'	=>'raw',
		'value'	=>'CHtml::link(Comments::model()->countCommentByPostId($data->id) . " thảo luận." . (Comments::model()->countNewCommentByPostId($data->id) > 0 ? "<br>Có " . Comments::model()->countNewCommentByPostId($data->id) . " thảo luận mới" : ""), Yii::app()->createUrl("comments/comments/index", array("post_id" => $data->id)))',
		'htmlOptions' => array(
                'style' => 'width:90px; text-align:center'
            )
		),

		array(
			'class'=>'CButtonColumn',
			'template' => '{view} {update}',
			'htmlOptions' => array(
                'style' => 'width:50px;text-align:center',
            )

		),
	),
)); ?>
</div>
<?php $this->endWidget(); ?>
<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('announcement-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('announcement-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-announcement").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('announcement-grid')
                    }
                });
                return false;
            });

        $('#delete-announcement').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Xóa dữ liệu được chọn?"))
                return false;
            }else{
                alert("Chưa chọn dữ liệu muốn xóa.");
                return false;
            }

            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                    	alert('Dữ liệu xóa thành công.');
                        $.fn.yiiGridView.update('announcement-grid');
                    } //success
                });//ajax
                return false;
        });

    });
</script>